
from configs import config
from views import BaseHandler
from models import goods_category


class CategoryRecommend(BaseHandler):

    async def get(self):
        """
        ---
        tags:
          - 商品分类
        summary: 推荐分类
        description:
        parameters:
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        res2 = await goods_category.get_second_category()
        res1 = await goods_category.get_first_category()
        new_res = {}
        for i in res2:
            i['img'] = config.IMG_PATH + i['img']
        new_res['first_category'] = res1
        new_res['second_category'] = res2
        self.write_json(new_res)


class CategoryGood(BaseHandler):

    async def get(self, id):
        """
        ---
        tags:
          - 商品分类
        summary: 一级分类
        description:
        parameters:
          - name: id
            in: path
            description: 一级商品分类id
            required: true
            schema:
              type: string
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        res = await goods_category.get_second_from_first(id)
        for i in res:
            i['img'] = config.IMG_PATH + i['img']
        return self.write_json(data=res)


class SecondCategoryGood(BaseHandler):

    async def get(self, id):
        """
        ---
        tags:
          - 商品分类
        summary: 二级分类
        description:
        parameters:
          - name: id
            in: path
            description: 二级商品分类id
            required: true
            schema:
              type: string
        responses:
          '200':
            description: successful operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/OkModel'
          '400':
            description: error operation
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/ErrorModel'
        """
        spus = await goods_category.get_spu_from_second(id)
        for spu in spus:
            spu['img'] = config.IMG_PATH + spu['img']
            spu['price'] = float(spu['price'])
        self.write_json(data=spus)

